package com.shenhaofeng.demo.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.ContentLoadingProgressBar;
import android.view.View;
import android.widget.TextView;

import com.shenhaofeng.app.component.BaseActivity;
import com.shenhaofeng.app.loader.content.ContentLoader;
import com.shenhaofeng.app.loader.content.ContentSource;
import com.shenhaofeng.app.loader.content.OnBindContentListener;
import com.shenhaofeng.app.loader.content.component.ContentLoadingComponent;
import com.shenhaofeng.app.loader.content.component.StatusSwitechComponent;
import com.shenhaofeng.app.widget.status.ViewStatusSwitcher;
import com.shenhaofeng.demo.R;

import java.util.concurrent.TimeUnit;

import rx.Observable;

/**
 * Created by haofeng on 2016/9/28.
 */

public class StatusSwitchActivity extends BaseActivity implements StatusSwitechComponent.OnErrorStatusListener, OnBindContentListener<String>, ContentSource<String> {


    private ContentLoader<String> contentLoader;

    private ViewStatusSwitcher viewStatusSwitcher;
    private TextView contentView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        contentLoader = new ContentLoader<>(new StatusSwitechComponent<String>(this));
        setContentView(R.layout.act_status_switch);
        viewStatusSwitcher = (ViewStatusSwitcher) findViewById(R.id.view_switcher);
        contentView = (TextView) findViewById(R.id.content_view);
        contentLoader.attach(viewStatusSwitcher, contentView, this, this);
        contentLoader.load();
        contentView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                contentLoader.load();
            }
        });

    }


    @Override
    public int onHandleError(Throwable e) {
        return 1;
    }

    @Override
    public Observable<String> onRequestContent(Bundle args) {
        return Observable.just("test").delay(2, TimeUnit.SECONDS);
    }

    @Override
    public void onBindContent(View contentView, String result) {
        this.contentView.setText(result);
    }
}
